/**
 * Project: greenline-rms-web-war
 * 
 * File Created at 2013-4-28
 * 
 * Copyright 2012 Greenline.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Greenline Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Greenline.com.
 */
package jetty;

import java.io.File;

import javax.naming.ConfigurationException;

import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.NCSARequestLog;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.server.handler.RequestLogHandler;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.util.log.StdErrLog;
import org.eclipse.jetty.webapp.WebAppContext;

/**
 * @Type Jetty
 * @Desc https请求的当前证书 域名为localhost
 * @author jianyun.zheng
 * @date 2013-4-28
 * @Version V1.0
 */
public class PayPortalJetty {
    /** port */
    private int port = 8088;

    protected String charset = "UTF-8";

    public PayPortalJetty() {
    }

    public PayPortalJetty(int port) {
        this.port = port;
    }

    public PayPortalJetty(int port, String charset) {
        super();
        this.port = port;
        this.charset = charset;
    }

    /**
     * 服务器启动。
     * 
     * @throws ConfigurationException
     */
    public void start() {

        // 设置Jetty日志
        System.setProperty("org.eclipse.jetty.util.log.class", StdErrLog.class.getName());

        HandlerCollection handlers = new HandlerCollection();

        Server server = new Server();

        // 设置连接器
        Connector connector = new SelectChannelConnector();
        connector.setPort(this.port);
        server.setConnectors(new Connector[] { connector });

        RequestLogHandler requestLogHandler = new RequestLogHandler();

        // 设置context
        WebAppContext context = new WebAppContext();
        context.setResourceBase("./src/main/webapp");
        //context.setContextPath("/");
        context.setDefaultsDescriptor("src/test/java/jetty/webdefault.xml");
        // PS:嵌入式的Jetty，应用当前工程的ClassPath，如果不设置将使用WebAppClassLoder，WEB-INF/lib目录加载jar。
        context.setClassLoader(Thread.currentThread().getContextClassLoader());
        context.setParentLoaderPriority(true);

        handlers.setHandlers(new Handler[] { context, new DefaultHandler(), requestLogHandler });
        server.setHandler(handlers);

        File logFile = new File("e:/rms-jetty.log");

        NCSARequestLog requestLog = new NCSARequestLog(logFile.getAbsolutePath());
        requestLog.setExtended(false);
        requestLogHandler.setRequestLog(requestLog);
        server.setSendServerVersion(true);
        // 启动Server
        try {
            server.start();
            server.join();
        } catch (Exception e) {
        }

    }

    public static void main(String[] args) {
        PayPortalJetty server = new PayPortalJetty();
        server.start();
    }
}
